package com.guo.proxy;

import com.guo.rpc.client.RpcClientStub;
import com.guo.rpc.client.handler.ServiceRemoteInvokeResponseHandler;
import com.guo.rpc.codec.ServiceRemoteInvokeRequest;
import io.netty.channel.Channel;

import java.lang.reflect.Proxy;
import java.util.concurrent.*;

/**
 * @Auther: hotlove_linx
 * @Date: 2021/7/18 17:22
 * @Description:
 */
public class ProxyFactory {

    private ExecutorService executorService = new ThreadPoolExecutor(5, 10, 10,
            TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));

    public Object getProxy(Class<?> serviceClass, Class<?> implServiceClass) {
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{serviceClass},
                (proxy, method, args) -> {
                    RpcClientStub rpcClientStub = new RpcClientStub();
                    System.out.println(implServiceClass.getSimpleName());
                    System.out.println(method.getName());

                    ServiceRemoteInvokeRequest req = new ServiceRemoteInvokeRequest();
                    req.setVersion("1.0");
                    req.setMethodName(method.getName());
                    req.setServiceName(implServiceClass.getSimpleName());
                    req.setArgs(args);

                    rpcClientStub.initRpcClient(req);



                    return executorService.submit(ServiceRemoteInvokeResponseHandler.INSTANCE).get();
                });
    }

}
